package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/list")
public class ListApiController {
    //ES官方
//    @Autowired
//    private TransportClient transportClient;//低级别客户端
    @Autowired
    private RestHighLevelClient restHighLevelClient;//高级别客户端

    //spring官方封装
//    @Autowired
//    private ElasticsearchTemplate elasticsearchTemplate;//低级别客户端
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//高级别客户端

    @Autowired
    private ListService listService;

    //创建:索引库 类型 文档 域
    @GetMapping("/createIndex")
    public Result createIndex() {
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    //上架
    @PostMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        listService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @PostMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        listService.cancelSale(skuId);
        return Result.ok();
    }

    //商品热度评分 加分 没必要加事务 没必要有返回值
    @PutMapping("/inner/incrHotScore/{skuId}/{score}")
    public void incrHotScore(@PathVariable Long skuId, @PathVariable Integer score) {
        listService.incrHotScore(skuId, score);
    }

    //开始搜索  数据较多且传递的是JSON,大对象要用POST请求方式
    @PostMapping("/inner/search")
    //SearchResponseVo即是返回值也是入参
    public SearchResponseVo search(@RequestBody SearchParam searchParam) {
        SearchResponseVo searchResponseVo = listService.search(searchParam);
        return searchResponseVo;
    }
}
